/** 
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.pdlime;

import java.util.Hashtable;


import lime2.LimeServerID;
import lime2.LimeServer;

/**
 * Maintain the performance driven policies items. This manager is a cache for
 * all META tuple on current tuple space that defines the policies. Through
 * access this object, rather than "rd" or "rdp" META tuples, performance on time is
 * improved (Storage occupancy is redundant as the cost). This class is also
 * responsible of performing policy switch dynamically.
 * 
 * @author Pan Wenhui
 * @author Xing Jiankuan
 * 
 */
class PDPolicyMgr {

	private PDPolicyMgr(String type) {
		super();
		// Create and put the initial policy item for META and ANYTYPE tuples.
		// Since they will be set at the start of system, no policy switch is
		// needed, so we directly put it rather than use <code>apply</code>.
		PDPolicyItem metaPolicy = new PDPolicyItem(SystemTupleType.META, LimeServer.getServer().getServerID(), PDPolicy.FR, PDPolicy.ANY);
		items.put(new PDPolicyKey(metaPolicy.getTargetType(), metaPolicy.getTargetHost()), metaPolicy);
		PDPolicyItem anyPolicy = new PDPolicyItem(SystemTupleType.ANYTYPE, LimeServer.getServer().getServerID(), PDPolicy.SO, PDPolicy.MASTER);
		items.put(new PDPolicyKey(anyPolicy.getTargetType(), anyPolicy.getTargetHost()), anyPolicy);
	}

	// Map< <TupleType, HostID>, <RPPolicyItem>
	private Hashtable items = new Hashtable();


	/**
	 * Apply a policy item to the manager. If the specified policy target has been
	 * applied a policy, and if the new and old one are different, the old one is 
	 * replaced.  If the  policy target didn't apply a policy, the new policy is
	 * added. In both cases, a policy switch process is started. After that, this
	 * method returns <code>true</code>. If the new policy is same as the old one,
	 * nothing happens, and <code>false</code> will be returned. <br>
	 * For details of policy item, see {@link sagroup.pdlime.PDPolicyItem}.
	 * 
	 * @param policyItem the policy item to be applied
	 * @return true means a successful policy application and false means failed
	 * @author Xing Jiankuan
	 */
	public boolean apply(PDPolicyItem policyItem) {
		PDPolicyKey key = new PDPolicyKey(policyItem.getTargetType(), policyItem.getTargetHost());
		PDPolicyItem oldItem = (PDPolicyItem)items.get(key);
		//TODO: compare the old and new policy. If the old one is not found, it means
		//the default policy is applied, that is SO, MASTER.
		//TODO: If the old one is different with the new one, start policy switch
		items.put(key, policyItem);
		return true;
	}

	public PDPolicy getPolicy(TupleType targetType, LimeServerID host) {
		return null;
	}
	
	
}

/**
 * immutable class.
 * 
 * @author Wenhui Pan
 * 
 */
class PDPolicyKey {
	final TupleType tupleType;
	final LimeServerID hostId;

	PDPolicyKey(TupleType tupleType, LimeServerID hostId) {
		this.tupleType = tupleType;
		this.hostId = hostId;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((hostId == null) ? 0 : hostId.hashCode());
		result = prime * result
				+ ((tupleType == null) ? 0 : tupleType.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final PDPolicyKey other = (PDPolicyKey) obj;
		if (hostId == null) {
			if (other.hostId != null)
				return false;
		} else if (!hostId.equals(other.hostId))
			return false;
		if (tupleType == null) {
			if (other.tupleType != null)
				return false;
		} else if (!tupleType.equals(other.tupleType))
			return false;
		return true;
	}
}